package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.BusiHistoryMapper;
import com.hq.ims.data.entity.BusiHistory;
import com.hq.ims.data.entity.BusiInfo;
import com.hq.ims.data.entityenum.EnumAuditStatus;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.BusiChangeTimeRecordMapstruct;
import com.hq.ims.data.vo.BusiHistoryMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 经营信息管理历史表 服务实现类
 * @since 20241025
 */
@Service
public class BaseBusiHistoryService extends BaseService<BusiHistoryMapper, BusiHistory> {
    @Resource
    private BaseBusiChangeTimeRecordService baseBusiChangeTimeRecordService;

    /**
     * 分页
     */
    public List<BusiHistoryMapstruct.BusiHistoryVo> getByPage(
            RequestPage page,
            BusiHistoryMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<BusiHistory> lq = this.lambdaQuery();
        //排序
        lq.orderByDesc(BusiHistory::getCreateDateTime);
        //查询条件

        //执行分页
        lq.page(page);

        List<BusiHistory> dataList = page.getRecords();
        List<BusiHistoryMapstruct.BusiHistoryVo> voList = BusiHistoryMapstruct.INSTANCE.toBusiHistoryVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiHistory addData(BusiHistoryMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        BusiHistory entity = new BusiHistory();
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setRepetitionRate(vo.getRepetitionRate());
        entity.setProjectName(vo.getProjectName());
        entity.setInfoType(vo.getInfoType());
        entity.setInfoSource(vo.getInfoSource());
        entity.setProjectAddressName(vo.getProjectAddressName());
        entity.setLongitude(vo.getLongitude());
        entity.setLatitude(vo.getLatitude());
        entity.setProjectStage(vo.getProjectStage());
        entity.setProvince(vo.getProvince());
        entity.setProvinceCode(vo.getProvinceCode());
        entity.setCity(vo.getCity());
        entity.setCityCode(vo.getCityCode());
        entity.setContactPerson(vo.getContactPerson());
        entity.setContactInfomation(vo.getContactInfomation());
        entity.setOwnerCompanyName(vo.getOwnerCompanyName());
        entity.setConstructionName(vo.getConstructionName());
        entity.setOutputScale(vo.getOutputScale());
        entity.setProjectRemark(vo.getProjectRemark());
        entity.setPublisher(vo.getPublisher());
        entity.setPublisherId(vo.getPublisherId());
        entity.setPublishStatus(vo.getPublishStatus());
        entity.setPublishTime(vo.getPublishTime());
        entity.setAuditStatus(vo.getAuditStatus());
        entity.setAuditUserId(vo.getAuditUserId());
        entity.setAuditTime(vo.getAuditTime());
        entity.setAuditRemark(vo.getAuditRemark());
        entity.setAcceptStatus(vo.getAcceptStatus());
        entity.setOrgId(vo.getOrgId());
        entity.setOrgName(vo.getOrgName());
        entity.setParentId(vo.getParentId());
        entity.setRootId(vo.getRootId());
        entity.setIndustryAssign(vo.getIndustryAssign());
        entity.setCompanyDivideIntoRuleValue(vo.getCompanyDivideIntoRuleValue());
        entity.setFundingSource(vo.getFundingSource());
        entity.setTenderClosingDate(vo.getTenderClosingDate());
        entity.setDeadlineForBidding(vo.getDeadlineForBidding());
        entity.setProjectScale(vo.getProjectScale());
        entity.setDurationRequired(vo.getDurationRequired());
        entity.setTenderScope(vo.getTenderScope());
        entity.setQualifications(vo.getQualifications());
        entity.setTenderControlPrice(vo.getTenderControlPrice());
        entity.setBond(vo.getBond());
        entity.setBondEndTime(vo.getBondEndTime());
        entity.setCreateDateTime(vo.getCreateDateTime());
        entity.setUpdateDateTime(vo.getUpdateDateTime());
        entity.setIsDel(vo.getIsDel());
        entity.setCreateUserName(vo.getCreateUserName());
        entity.setUpdateUserName(vo.getUpdateUserName());
        entity.setVersion(vo.getVersion());

        entity.insert();
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiHistory editData(BusiHistoryMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);

        //账值新的对象
        BusiHistory entity = this.getById(vo.getBusiHistoryId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setRepetitionRate(vo.getRepetitionRate());
        entity.setProjectName(vo.getProjectName());
        entity.setInfoType(vo.getInfoType());
        entity.setInfoSource(vo.getInfoSource());
        entity.setProjectAddressName(vo.getProjectAddressName());
        entity.setLongitude(vo.getLongitude());
        entity.setLatitude(vo.getLatitude());
        entity.setProjectStage(vo.getProjectStage());
        entity.setProvince(vo.getProvince());
        entity.setProvinceCode(vo.getProvinceCode());
        entity.setCity(vo.getCity());
        entity.setCityCode(vo.getCityCode());
        entity.setContactPerson(vo.getContactPerson());
        entity.setContactInfomation(vo.getContactInfomation());
        entity.setOwnerCompanyName(vo.getOwnerCompanyName());
        entity.setConstructionName(vo.getConstructionName());
        entity.setOutputScale(vo.getOutputScale());
        entity.setProjectRemark(vo.getProjectRemark());
        entity.setPublisher(vo.getPublisher());
        entity.setPublisherId(vo.getPublisherId());
        entity.setPublishStatus(vo.getPublishStatus());
        entity.setPublishTime(vo.getPublishTime());
        entity.setAuditStatus(vo.getAuditStatus());
        entity.setAuditUserId(vo.getAuditUserId());
        entity.setAuditTime(vo.getAuditTime());
        entity.setAuditRemark(vo.getAuditRemark());
        entity.setAcceptStatus(vo.getAcceptStatus());
        entity.setOrgId(vo.getOrgId());
        entity.setOrgName(vo.getOrgName());
        entity.setParentId(vo.getParentId());
        entity.setRootId(vo.getRootId());
        entity.setIndustryAssign(vo.getIndustryAssign());
        entity.setCompanyDivideIntoRuleValue(vo.getCompanyDivideIntoRuleValue());
        entity.setFundingSource(vo.getFundingSource());
        entity.setTenderClosingDate(vo.getTenderClosingDate());
        entity.setDeadlineForBidding(vo.getDeadlineForBidding());
        entity.setProjectScale(vo.getProjectScale());
        entity.setDurationRequired(vo.getDurationRequired());
        entity.setTenderScope(vo.getTenderScope());
        entity.setQualifications(vo.getQualifications());
        entity.setTenderControlPrice(vo.getTenderControlPrice());
        entity.setBond(vo.getBond());
        entity.setBondEndTime(vo.getBondEndTime());
        entity.setCreateDateTime(vo.getCreateDateTime());
        entity.setUpdateDateTime(vo.getUpdateDateTime());
        entity.setIsDel(vo.getIsDel());
        entity.setCreateUserName(vo.getCreateUserName());
        entity.setUpdateUserName(vo.getUpdateUserName());
        entity.setVersion(vo.getVersion());

        entity.updateById();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String busiHistoryId) {
        BusiHistory data = this.getById(busiHistoryId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public BusiHistoryMapstruct.BusiHistoryVo getDetailVo(String busiHistoryId) {
        BusiHistory entity = this.getById(busiHistoryId);
        BusiHistoryMapstruct.BusiHistoryVo vo = BusiHistoryMapstruct.INSTANCE.toBusiHistoryVo(entity);
        return vo;
    }

    /**
     * @description: 保存产业标记变更记录
     *
     * @author: yinzhangxiao
     * @date: 2024/11/15 15:17
     */
    public void saveBusiChangeTimeRecord(BusiInfo busiInfo) {
        BusiHistory history = this.lambdaQuery()
                .eq(BusiHistory::getBusiInfoId, busiInfo.getBusiInfoId())
                .orderByDesc(BusiHistory::getCreateDateTime)
                .last(LocalConstant.LIMIT_1)
                .one();
        //插入历史表
        BusiHistoryMapstruct.SaveVo build = new BusiHistoryMapstruct.SaveVo();
        BeanUtils.copyProperties(busiInfo, build);
        BusiHistory busiHistory = this.addData(build);
        //插入信息变更历史记录表
        BusiChangeTimeRecordMapstruct.SaveVo changeVo = new BusiChangeTimeRecordMapstruct.SaveVo();
        changeVo.setChangeTime(LocalDateTime.now());
        changeVo.setChangeBeforeId(history.getBusiHistoryId());
        changeVo.setChangeAfterId(busiHistory.getBusiHistoryId());
        changeVo.setBusiInfoId(busiInfo.getBusiInfoId());
        changeVo.setAuditStatus(EnumAuditStatus.UnderReview);
        baseBusiChangeTimeRecordService.addData(changeVo);
    }
}